package com.idlecode.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.idlecode.bean.FieldTypeEntity;
import com.idlecode.bean.TableFieldEntity;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: IdleCode
 * @description
 * @author: rongjuwei
 * @create: 2024-02-18 13:50
 **/
public class FieldTypeService {

    private static List<FieldTypeEntity> fieldTypes;

    public static List<FieldTypeEntity> getFieldTypes() {
        if (CollUtil.isEmpty(fieldTypes)) {
            init();
        }
        return fieldTypes;
    }

    public static Map<String, FieldTypeEntity> getMap() {
        if (CollUtil.isEmpty(fieldTypes)) {
            init();
        }
        Map<String, FieldTypeEntity> map = new LinkedHashMap<>(fieldTypes.size());
        for (FieldTypeEntity entity : fieldTypes) {
            map.put(entity.getColumnType().toLowerCase(), entity);
        }
        return map;
    }

    private static final String fieldTypesStr =
            "datetime|Date|java.util.Date;" +
            "date|Date|java.util.Date;" +
            "tinyint|Integer|;" +
            "smallint|Integer|;" +
            "mediumint|Integer|;" +
            "int|Integer|;" +
            "integer|Integer|;" +
            "bigint|Long|;" +
            "float|Float|;" +
            "double|Double|;" +
            "decimal|BigDecimal|java.math.BigDecimal;" +
            "bit|Boolean|;" +
            "char|String|;" +
            "varchar|String|;" +
            "tinytext|String|;" +
            "text|String|;" +
            "mediumtext|String|;" +
            "longtext|String|;" +
            "timestamp|Date|java.util.Date;" +
            "NUMBER|Integer|;" +
            "NUMBER(-10,0)|Integer|;" +
            "NUMBER(11-,0)|Long|;" +
            "NUMBER(-,1-6)|Float|;" +
            "NUMBER(-,7-)|Double|;" +
            "BINARY_INTEGER|Integer|;" +
            "BINARY_FLOAT|Float|;" +
            "BINARY_DOUBLE|Double|;" +
            "VARCHAR2|String|;" +
            "NVARCHAR|String|;" +
            "NVARCHAR2|String|;" +
            "CLOB|String|;" +
            "int8|Long|;" +
            "int4|Integer|;" +
            "int2|Integer|;" +
            "REAL|Float|;" +
            "numeric|BigDecimal|java.math.BigDecimal;" +
            "numeric(-10,0)|Integer|;" +
            "numeric(11-,0)|Long|;" +
            "numeric(-,1-6)|Float|;" +
            "numeric(-,7-)|Double|";

    private static void init() {
        fieldTypes = new ArrayList<>();
        String[] rows = fieldTypesStr.split(";");
        if (rows != null && rows.length > 0) {
            for (String row : rows) {
                String[] column = row.split("\\|");
                if (column != null && column.length > 0) {
                    FieldTypeEntity fieldTypeEntity = new FieldTypeEntity();
                    fieldTypeEntity.setColumnType(column[0]);
                    fieldTypeEntity.setAttrType(column[1]);
                    if (column.length ==3) {
                        fieldTypeEntity.setPackageName(column[2]);
                    }
                    fieldTypes.add(fieldTypeEntity);
                }
            }
        }
    }

    public static List<String> getPackage(List<TableFieldEntity> fieldList) {
        if (CollUtil.isEmpty(fieldTypes)) {
            init();
        }
        List<String> importList = new ArrayList<>();
        for (TableFieldEntity tableFieldEntity : fieldList) {
            //跳过基类字段
            if (tableFieldEntity.isBaseField()) {
                continue;
            }
            String fieldType = tableFieldEntity.getFieldType();
            if (StrUtil.isNotBlank(fieldType)) {
                FieldTypeEntity fieldTypeEntity = fieldTypes.stream().filter(value -> fieldType.toLowerCase().equals(value.getColumnType().toLowerCase())).findFirst().orElse(null);
                if (fieldTypeEntity != null) {
                    String packageName = fieldTypeEntity.getPackageName();
                    if (StrUtil.isNotBlank(packageName)) {
                        importList.add(packageName);
                    }
                }
            }
        }
        return importList.stream().distinct().collect(Collectors.toList());
    }
}
